  <?php
  	//error_reporting(E_ERROR);
  	require_once("../Scripts/DatabaseConnect.php");
	$con=Remoteconnect();
	if($con==null){					
		$con=Localconnect();						
	}
	
	
	
function RSI($StartDate, $EndDate, $input, $Symbol, $con){
	$days=14;
	if(isset($input)&&$input!="none"){
		$days=$input;

	}
	$RSIarray=array();
	$RSIdata=array();

	$indicatorsDate=obtainDates((int)$days+1,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($RSIdata, floatval($row['Close']));
									
		}
			
	}
		
		
		
		array_push($RSIarray, trader_rsi($RSIdata, (int)$days));	
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
		$good=array();
		foreach($RSIarray[0] as $new){
		array_push($good,$new);	
		}
		return $good;
	
}

function SAR ($StartDate, $EndDate, $input, $Symbol, $con){
		$sarDataHigh=array();
		$sarDataLow=array();
		$sarArray=array();
		$acceleration=0.02;
		$Maximum=0.2;
		if(isset ($input)&&$input!="none"){
			$input=explode("/",$input);
				if(isset($input[0])){
			
					$acceleration=$input[0];
				}
				if(isset($input[1])){
					$Maximum=$input[1];
				}
				
		}
		//calcualte SAR her
		$displaysDate=obtainDates(2,$con,$Symbol,$StartDate,$EndDate);					
		foreach($displaysDate as $date){
			
			
			$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


			while($row = mysqli_fetch_array($result)) {
				
				array_push($sarDataHigh, floatval($row['High']));
				array_push($sarDataLow, floatval($row['Low']));
			
			}
				
		}

			array_push($sarArray, trader_sar($sarDataHigh,$sarDataLow, $acceleration,$Maximum));
		
			return $sarArray;
	
}

function WMA($StartDate, $EndDate, $input, $Symbol, $con){

	
	$days=20;
	if(isset($input)&&$input!="none"){
		$days=$input;

	}
	$WMAarray=array();
	$WMAdata=array();

	$indicatorsDate=obtainDates((int)$days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($WMAdata, floatval($row['Close']));
									
		}
			
	}
		
		
		
		array_push($WMAarray, trader_wma($WMAdata, (int)$days));

		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
		$good=array();
		foreach($WMAarray[0] as $new){
		array_push($good,$new);	
		}
		

		return $good;
	
}

function EMA($StartDate, $EndDate, $input, $Symbol, $con){
	
	$days=20;
	if(isset($input)&&$input!="none"){
		$days=$input;

	}
	$EMAarray=array();
	$EMAdata=array();

	$indicatorsDate=obtainDates((int)$days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($EMAdata, floatval($row['Close']));
									
		}
			
	}
		

		array_push($EMAarray, trader_ema($EMAdata, (int)$days));
	
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
		$good=array();
		foreach($EMAarray[0] as $new){
		array_push($good,$new);	
		}
		

		return $good;

	
}

function SMA($StartDate, $EndDate, $input, $Symbol, $con){
	
	$days=20;
	if(isset($input)&&$input!="none"){
		$days=$input;

	}
	$SMAarray=array();
	$SMAdata=array();

	$indicatorsDate=obtainDates((int)$days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($SMAdata, floatval($row['Close']));
									
		}
			
	}

		array_push($SMAarray, trader_sma($SMAdata, (int)$days));

		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
		$good=array();
		foreach($SMAarray[0] as $new){
		array_push($good,$new);	
		}
		

		return $good;

}



//Queries database to get the previous $number rows dates
function obtainDates($number,$con,$symbol,$startDate,$endDate){
	$dates=array();
	$newStartDate="";
	//get the correct starting date- eg.SMA: number dates to average
	
		$result = mysqli_query($con,"SELECT * FROM (
	SELECT * FROM `shares` WHERE `Symbol`='$symbol' AND `Date`<'$startDate' Order by `Date` DESC Limit $number
	)sub
	Order by `Date` asc limit 1
	");
		
		
				while($row = mysqli_fetch_array($result)) {
					$newStartDate=$row['Date'];
						
				
	// obtain array of dates to send back to the javascript
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$symbol' AND `Date` BETWEEN '$newStartDate' AND '$endDate'");
		
		
				while($row = mysqli_fetch_array($result)) {
					
					array_push($dates, $row['Date']);
					
		
				}	
	}
		$dates=array_unique($dates);//remove duplicated values
		return $dates;
}		

function Bollinger ($StartDate, $EndDate, $data, $Symbol, $con){
		$bollingerData=array();
		$bollingerArray=array();
		$SMA=20;
		$StandardDeviation=2;
		
		if(isset ($data)&&$data!="none"){
			$input=explode("/",$data);
				if(isset($input[0])){
			
					$SMA=$input[0];
				}
				if(isset($input[1])){
					$StandardDeviation=$input[1];
				}
				
		}
		
		
							
		$displaysDate=obtainDates($SMA,$con,$Symbol,$StartDate,$EndDate);
		
		foreach($displaysDate as $date){
			
			$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


			while($row = mysqli_fetch_array($result)) {
				
				array_push($bollingerData, floatval($row['Close']));
				
			
			}
				
		}
			
			
			array_push($bollingerArray, trader_bbands ($bollingerData, $SMA, $StandardDeviation, $StandardDeviation , TRADER_MA_TYPE_SMA));
			
		//[0][0-2]
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		$good=array();
		$good1=array();
		$good2=array();
		$good3=array();
		
		foreach($bollingerArray[0][0] as $new){
				
			array_push($good1,$new);
		}

		foreach($bollingerArray[0][1] as $new){
				
			array_push($good2,$new);
		}

		foreach($bollingerArray[0][2] as $new){
				
			array_push($good3,$new);
		}
		array_push($good,$good1);
		array_push($good,$good2);
		array_push($good,$good3);
		return $good;
		}
		
function MacD ($StartDate, $EndDate, $data, $Symbol, $con){
	$fastperiod=12;
	$slowperiod=26;
	$signalperiod=9;
	$MacDdata=array();
	$MacDarray=array();
	if(isset ($data)&&$data!="none"){
		$input=explode("/",$data);
			if(isset($input[0])){
		
				$fastperiod=$input[0];
			}
			if(isset($input[1])){
				$slowperiod=$input[1];
			}
			if(isset($input[2])){
				$signalperiod=$input[2];
			}
			
	}	
	$days=$slowperiod+$signalperiod-1;
	$indicatorsDate=obtainDates($days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($MacDdata, floatval($row['Close']));
									
		}
			
	}

		array_push($MacDarray, trader_macd($MacDdata, $fastperiod,$slowperiod,$signalperiod));
		//[0][0-2]
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		$good=array();
		$good1=array();
		$good2=array();
		$good3=array();
		
		foreach($MacDarray[0][0] as $new){
				
			array_push($good1,$new);
		}
		foreach($MacDarray[0][1] as $new){
				
			array_push($good2,$new);
		}
		foreach($MacDarray[0][2] as $new){
				
			array_push($good3,$new);
		}
		array_push($good,$good1);
		array_push($good,$good2);
		array_push($good,$good3);
		return $good;				
}

function ROC ($StartDate, $EndDate, $data, $Symbol, $con){
	$days=12;
	$ROCdata=array();
	$ROCarray=array();
	if(isset($data)&&$data!="none"){
			
		$days=$data;
	}	
	$indicatorsDate=obtainDates(12,$con,$Symbol,$StartDate,$EndDate);	
				
	foreach($indicatorsDate as $date){
		
		
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($ROCdata, floatval($row['Close']));
			
									
		}
			
	}
		
		
		
		array_push($ROCarray, trader_roc($ROCdata, 12));
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
	
		$good=array();
		foreach($ROCarray[0] as $new){
		array_push($good,$new);	
		}
		return $good;
}

function OBV ($StartDate, $EndDate, $data, $Symbol, $con){
	$OBVdata=array();
	$OBVdata2=array();
	$OBVarray=array();
	$indicatorsDate=obtainDates(1,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($OBVdata, floatval($row['Close']));
			array_push($OBVdata2, floatval($row['Volume']));
									
		}
			
	}

		array_push($OBVarray, trader_obv($OBVdata, $OBVdata2));
		
		return $OBVarray;
}

function WilliamR ($StartDate, $EndDate, $data, $Symbol, $con){
	$days=14;
	$WilliamRhighdata=array();
	$WilliamRlowdata=array();
	$WilliamRclosedata=array();
	$WilliamRarray=array();
	if(isset($data)&&$data!="none"){
			
		$days=$data;
	}	
	$indicatorsDate=obtainDates($days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($WilliamRhighdata, floatval($row['High']));
			array_push($WilliamRlowdata, floatval($row['Low']));
			array_push($WilliamRclosedata, floatval($row['Close']));
									
		}
			
	}

		array_push($WilliamRarray, trader_willr($WilliamRhighdata,$WilliamRlowdata,$WilliamRclosedata, $days));
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
		$good=array();
		foreach($WilliamRarray[0] as $new){
		array_push($good,$new);	
		}
		return $good;
}

function MTM ($StartDate, $EndDate, $data, $Symbol, $con){
	$days=9;
	$MTMdata=array();
	$MTMarray=array();
	if(isset($data)&&$data!="none"){
			
		$days=$data;
	}	
	$indicatorsDate=obtainDates($days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($MTMdata, floatval($row['Close']));
									
		}
			
	}

		array_push($MTMarray, trader_mom($MTMdata, $days));
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
		$good=array();
		foreach($MTMarray[0] as $new){
		array_push($good,$new);	
		}
		return $good;

}

function FastSTC ($StartDate, $EndDate, $data, $Symbol, $con){
	$fastK=20;
	$fastD=5;
	$FastSTChighdata=array();
	$FastSTClowdata=array();
	$FastSTCclosedata=array();
	$FastSTCarray=array();
	if(isset ($data)&&$data!="none"){
		$input=explode("/",$data);
			if(isset($input[0])){
		
				$fastK=$input[0];
			}
			if(isset($input[1])){
				$slowD=$input[1];
			}
	$days = $fastK+$fastD-1;
	$indicatorsDate=obtainDates($days,$con,$Symbol,$StartDate,$EndDate);					
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			array_push($FastSTChighdata, floatval($row['High']));
			array_push($FastSTClowdata, floatval($row['Low']));
			array_push($FastSTCclosedata, floatval($row['Close']));
									
		}
			
	}
		
		
		array_push($FastSTCarray, trader_stoch ($FastSTChighdata , $FastSTClowdata, $FastSTCclosedata, $fastK, $fastK, TRADER_MA_TYPE_SMA, $fastD, TRADER_MA_TYPE_SMA));
		print_r($FastSTCarray);
		$FastSTCarrays =array();

		array_push($FastSTCarrays, trader_stochf($FastSTChighdata, $FastSTClowdata, $FastSTCclosedata, $fastK, $fastD, TRADER_MA_TYPE_SMA));
		print_r($FastSTCarrays);

		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
	
		$good=array();
		$good1=array();
		$good2=array();
		foreach($FastSTCarray[0][0] as $new){
		array_push($good1,$new);	
		}
		foreach($FastSTCarray[0][1] as $new){
		array_push($good2,$new);	
		}
		array_push($good,$good1);
		array_push($good,$good2);
		return $good;
	
	}
}

function DMI ($StartDate, $EndDate, $data, $Symbol, $con){
	$days=14;
	$DMIplushighdata=array();
	$DMIpluslowdata=array();
	$DMIplusclosedata=array();
	$DMIminushighdata=array();
	$DMIminuslowdata=array();
	$DMIminusclosedata=array();
	$DMIarray=array();
	if(isset($data)&&$data!="none"){
			
		$days=$data;
	}	
	
	$indicatorsDate=obtainDates($days,$con,$Symbol,$StartDate,$EndDate);	
				
	foreach($indicatorsDate as $date){
		
		
		$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");


		while($row = mysqli_fetch_array($result)) {
			
			
			array_push($DMIplushighdata, floatval($row['High']));
			array_push($DMIpluslowdata, floatval($row['Low']));
			array_push($DMIplusclosedata, floatval($row['Close']));
			array_push($DMIminushighdata, floatval($row['High']));
			array_push($DMIminuslowdata, floatval($row['Low']));
			array_push($DMIminusclosedata, floatval($row['Close']));
									
		}
			
	}
		
		
		array_push($DMIarray, trader_plus_dm($DMIplushighdata,$DMIpluslowdata,(int)$days));
		array_push($DMIarray, trader_minus_dm($DMIminushighdata,$DMIminuslowdata,(int)$days));
		
		//fix the stupid array(pecl trader array does not start at index 0, i am fixing that.
		//[0]
	
		$good=array();
		$good1=array();
		$good2=array();
		foreach($DMIarray[0] as $new){
		array_push($good1,$new);	
		}
		foreach($DMIarray[1] as $new){
		array_push($good2,$new);	
		}
		array_push($good,$good1);
		array_push($good,$good2);
		return $good;
	
}

function PVT ($StartDate, $EndDate, $data, $Symbol, $con){
//((Close-yesturdays Close/yesturdays close) *volume) +yesturdays pvt
$count=0;
$PVTarray=array();
$yesturdaysClose="";
$yesturdayPVT=0;
$indicatorsDate=obtainDates(1,$con,$Symbol,$StartDate,$EndDate);					
foreach($indicatorsDate as $date){
	
	
	$result = mysqli_query($con,"SELECT * FROM  `shares` WHERE `Symbol`='$Symbol' AND `Date`='$date'");

		
	while($row = mysqli_fetch_array($result)) {
	$PVT=0;
	$close = $row['Close'];
	
	$volume = $row['Volume'];
	if($count>0){
		$PVT=0;
		$PVT=((($close-$yesturdaysClose)/$yesturdaysClose)*$volume)+$yesturdayPVT;

	array_push($PVTarray,$PVT);	
	
	}
	$count+=1;			
	$yesturdaysClose=$close;	
	$yesturdayPVT=$PVT;
	
	}
		
}

	return $PVTarray;
}

	?>